package com.example.administrator.netchess;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyService extends Service {

    private MyJson myJson = new MyJson();
    private PrintWriter pw = null;
    private boolean isClose = false;
    private Handler handler = null;
    private ExecutorService es = Executors.newFixedThreadPool(2);
    private OptBinder binder = new OptBinder();
    private String data ;


    public MyService() {
    }

    public class OptBinder extends Binder {
        public void sendData (String data) {
         SendDataThread sendDataThread = new SendDataThread(pw,data);
            es.execute(sendDataThread);
        }

        public void setHandle (Handler handle) {
            MyService.this.handler = handle;
        }

    }

    @Override
    public void onCreate() {
        super.onCreate();

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {

                    Socket socketClient = new Socket("192.168.3.232",12345);
                    InputStream in = socketClient.getInputStream();
                    OutputStream out = socketClient.getOutputStream();

                    InputStreamReader isr = new InputStreamReader(in,"UTF-8");
                    BufferedReader br = new BufferedReader(isr);

                    OutputStreamWriter osw = new OutputStreamWriter(out,"UTF-8");
                    BufferedWriter bw = new BufferedWriter(osw);
                    pw = new PrintWriter(bw);

                    while (!isClose){
                        data = br.readLine();
                        System.out.println("--data="+data.toString());
                        if (null!=data) {

                            switch (myJson.GetCmd(data)) {

                                case Common.JsonCmdResp.CREATE_ROOM:
                                    myJson.GetBuildRoom(data,handler);
                                    break;

                                case Common.JsonCmdResp.APPLY_FOR_ROOM_LIST:
                                    myJson.GetRoomList(data,handler);
                                    break;

                                case Common.JsonCmdResp.APPLY_FOR_JOIN_ROOM:

                                    int status = 0;
                                        JSONObject jo = new JSONObject(data);;
                                        try {
                                          status= jo.getInt(Common.FinalString.KEY_STATUS);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    if (status==1){
                                         handler.sendEmptyMessage(Common.FinalString.MESSAGE_EMPTY);
                                     }else {
                                         handler.sendEmptyMessage(Common.FinalString.MESSAGE_INTENT_ROOM);
                                     }

                                    Message message = new Message();
                                    message.what = Common.FinalString.MESSAGE_ROOM_ACTIVITY;
                                    message.arg1 = status;
                                    handler.sendMessage(message);

                                    break;

                                case Common.JsonCmdResp.START_GAME:

                                    JSONObject jsonObject = new JSONObject(data);
                                    boolean my_ready = jsonObject.getBoolean(Common.FinalString.IS_READY);
                                    boolean opponent_ready = jsonObject.getBoolean(Common.FinalString.IS_OPPONENT_READY);
                                    boolean is_white_chess = jsonObject.getBoolean(Common.FinalString.IS_WHITE_CHESS);

                                    if (my_ready==true&&opponent_ready==true){
                                        Message message_start = new Message();
                                        message_start.what = Common.FinalString.MESSAGE_START_GAME;
                                        message_start.obj = is_white_chess;
                                        handler.sendMessage(message_start);

                                    }else {
                                        break;
                                    }
                                    break;

                            }
                        }
                    }

                    pw.close();
                    pw = null;
                    bw.close();
                    osw.close();
                    out.close();

                    br.close();
                    isr.close();
                    in.close();

                    socketClient.close();

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }).start();

    }




    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onDestroy() {

        isClose = true;
        super.onDestroy();

    }
}
